package fdoj.eventInvitations.dao;

import java.util.List;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import fdoj.eventInvitations.entity.Contact;
import fdoj.eventInvitations.entity.Event;
import fdoj.eventInvitations.entity.Response;
/**
 * Klasa koja implementira CRUD operacije za entitet {@link Contact} 
 */
@Stateless
@Local(ContactDao.class)
public class ContactDaoBean extends GenericDaoBean<Contact, Integer> implements
		ContactDao {

	public List<Contact> findByOwner(Integer id) {
		List<Contact> retVal = null;
		try {
			Query q = em
					.createQuery("SELECT c FROM Contact c WHERE c.addressBook.id=:id  AND c.valid=TRUE");
			q.setParameter("id", id);
			retVal = (List<Contact>) q.getResultList();
		} catch (NoResultException ex) {

		}
		return retVal;
	}

	public void deleteById(Integer id) {
		Query q = em.createQuery("UPDATE Contact SET valid=FALSE WHERE id=:id");
		q.setParameter("id", id);
		q.executeUpdate();
	}

	@Override
	public List<Contact> contactsByEvent(Integer eventId, Response response) {
		List<Contact> retVal = null;
		try{
			Query q=em.createQuery("SELECT c FROM Contact c, Invantation i" +
					" WHERE i.event.id=:id AND i.eventCall.id=c.id AND i.status=:response");
			q.setParameter("id", eventId);
			q.setParameter("response", response);
			retVal=q.getResultList();
		}catch(NoResultException ex){
			
		}
		return retVal;
	}
}
